package com.forum.utils;/*
 *
 *作者：张继成
 *任务：
 *
 *备注：
 *@Date2023/05/22/9:09
 ***/

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MPUtil {
    public static final char UNDERLINE = '_';

    public static void main(String[] args) {
        System.out.println(camelToUnderline("ABCddfANM"));
    }

    public static Map camelToUnderlineMap(Map param,String pre){
        //自定义一个newMap对象
        Map<String,Object> newMap=new HashMap<String, Object>();
        //Iterator-java.util工具类的迭代器，用于循环
        Iterator<Map.Entry<String,Object>> it=param.entrySet().iterator();
        //hasnext()方法阻塞等待用户输入
        while(it.hasNext()){
            Map.Entry<String,Object> entry=it.next();
            String key=entry.getKey();
            String newKey=camelToUnderline(key);
            //endswith() 方法用于判断字符串是否以指定后缀结尾，如果以指定后缀结尾返回True，否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。
            if (pre.endsWith(".")){
                newMap.put(pre+newKey,entry.getValue());
            }else if(StringUtils.isEmpty(pre)){
                newMap.put(newKey,entry.getValue());
            }else{
                //做拼接以后put
                newMap.put(pre+"."+newKey,entry.getValue());
            }
        }
        return newMap;
    }

    /**
     * 驼峰格式字符串转换为下划线格式字符串
     *
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static Wrapper genLike(Wrapper wrapper, Map param) {
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        int i=0;
        while (it.hasNext()) {
            if(i>0) wrapper.and();
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            String value = (String) entry.getValue();
            wrapper.like(key, value);
            i++;
        }
        return wrapper;
    }

    //mybatis plus allEQ 表达式转换
    public static Map allEQMapPre(Object bean, String pre) {
        Map<String, Object> map = BeanUtil.beanToMap(bean);
        return camelToUnderlineMap(map,pre);
    }
    //mybatis plus allEQ 表达式转换
    public static Map allEQMap(Object bean) {
        Map<String, Object> map =BeanUtil.beanToMap(bean);
        return camelToUnderlineMap(map,"");
    }

    public static Wrapper allLikePre(Wrapper wrapper, Object bean, String pre) {
        Map<String, Object> map =BeanUtil.beanToMap(bean);
        Map result = camelToUnderlineMap(map,pre);

        return genLike(wrapper,result);
    }

    public static Wrapper allLike(Wrapper wrapper,Object bean) {
        Map result = BeanUtil.beanToMap(bean, true, true);
        return genLike(wrapper,result);
    }

    public static Wrapper genLikeOrEq( Wrapper wrapper,Map param) {
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        int i=0;
        while (it.hasNext()) {
            if(i>0) wrapper.and();
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            if(entry.getValue().toString().contains("%")) {
                wrapper.like(key, entry.getValue().toString().replace("%", ""));
            } else {
                wrapper.eq(key, entry.getValue());
            }
            i++;
        }
        return wrapper;
    }

    public static Wrapper likeOrEq(Wrapper wrapper,Object bean) {
        Map result = BeanUtil.beanToMap(bean, true, true);
        return genLikeOrEq(wrapper,result);
    }


    public static Wrapper genEq( Wrapper wrapper,Map param) {
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        int i=0;
        while (it.hasNext()) {
            if(i>0) wrapper.and();
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            wrapper.eq(key, entry.getValue());
            i++;
        }
        return wrapper;
    }

    public static Wrapper allEq(Wrapper wrapper,Object bean) {
        Map result = BeanUtil.beanToMap(bean, true, true);
        return genEq(wrapper,result);
    }

    public static Wrapper between(Wrapper wrapper,Map<String, Object> params) {
        for(String key : params.keySet()) {
            String columnName = "";
            if(key.endsWith("_start")) {
                columnName = key.substring(0, key.indexOf("_start"));
                if(org.apache.commons.lang3.StringUtils.isNotBlank(params.get(key).toString())) {
                    wrapper.ge(columnName, params.get(key));
                }
            }
            if(key.endsWith("_end")) {
                columnName = key.substring(0, key.indexOf("_end"));
                if(org.apache.commons.lang3.StringUtils.isNotBlank(params.get(key).toString())) {
                    wrapper.le(columnName, params.get(key));
                }
            }
        }
        return wrapper;
    }

    public static Wrapper sort(Wrapper wrapper,Map<String, Object> params) {
        String order = "";
        if(params.get("order") != null && org.apache.commons.lang3.StringUtils.isNotBlank(params.get("order").toString())) {
            order = params.get("order").toString();
        }
        if(params.get("sort") != null && org.apache.commons.lang3.StringUtils.isNotBlank(params.get("sort").toString())) {
            if(order.equalsIgnoreCase("desc")) {
                wrapper.orderDesc(Arrays.asList(params.get("sort")));
            } else {
                wrapper.orderAsc(Arrays.asList(params.get("sort")));
            }
        }
        return wrapper;
    }
}
